package cn.jx.asc.lhg.bzxq.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.jx.asc.lhg.bzxq.model.Leaderboard;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lhg
 */
public class LeaderboardDAO {
    private DataBaseOpenHelper helper;
    private SQLiteDatabase db;
    public LeaderboardDAO(Context context){
        helper = new DataBaseOpenHelper(context);

    }
    public void add(Leaderboard leaderboard){
        db=helper.getWritableDatabase();
        helper.onCreate(db);
        db.execSQL("insert into tb_test (id,name,integral,date) values(?,?,?,?)",new Object[]{
                leaderboard.getId(),leaderboard.getName(),leaderboard.getIntegral(),leaderboard.getDate()
        });
        //db.execSQL("insert into tb_points (id,name,integral) values(1,'jj',10)");
    }

    public void update(Leaderboard leaderboard){
        db=helper.getWritableDatabase();
        db.execSQL("update leaderboard set name=?,integral=? where id =?",new Object[]{
                leaderboard.getName(),leaderboard.getIntegral(),leaderboard.getId()
        });
    }

    public Leaderboard find(Integer id){
        db=helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select id,name,integral from tb_points where id =? ",new String[]
                {
                        String.valueOf(id)
                });
        if (cursor.moveToNext()){
            System.out.println("-------------------------------------------------------");
            System.out.println("===============================cursor"+cursor);
            return new Leaderboard(cursor.getString(cursor.getColumnIndex("id")),
                    cursor.getString(cursor.getColumnIndex("name")),
                    cursor.getInt(cursor.getColumnIndex("integral")),"");
        }
        return null;
    }
    public List<Leaderboard> findAll(){
        List<Leaderboard> list = new ArrayList<>();
        db=helper.getWritableDatabase();
        Cursor cursor = db.query("tb_test",null,null,null,null,null,"date DESC");
        if (cursor.getColumnCount()!=0){
            cursor.moveToNext();
            do {
                String id = cursor.getString(cursor.getColumnIndex("id"));
                System.out.println("333333333333"+id);
                String name = cursor.getString(cursor.getColumnIndex("name"));
                Integer integral = cursor.getInt(cursor.getColumnIndex("integral"));
                String date = cursor.getString(cursor.getColumnIndex("date"));
                Leaderboard leaderboard = new Leaderboard(id, name, integral,date);
                list.add(leaderboard);

            }while (cursor.moveToNext());
            System.out.println("444444444444444444444444444444444444"+list);
            return list;
        }
        return null;
    }


}
